package com.vo;

import com.jfinal.kit.JsonKit;

/**
 * 功能描述: 封装ajax返回
 */
public class AjaxResult {

	// 标记成功失败，默认0：成功，1：失败、用于alert，2：失败、用于confirm
	private int codeNum = 0;

	// 返回的中文消息
	private String message;

	//返回错误信息
	private String err;
	
	// 成功时携带的数据
	private Object data;
	
	//回复问题成功时携带的数据
	private Object rsm;
	
	public int getCodeNum() {
		return codeNum;
	}

	public String getMessage() {
		return message;
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public String getErr() {
		return err;
	}

	public void setErr(String err) {
		this.err = err;
	}

	public Object getRsm() {
		return rsm;
	}

	public void setRsm(Object rsm) {
		this.rsm = rsm;
	}

	public void setCodeNum(int codeNum) {
		this.codeNum = codeNum;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	// 校验错误
	public boolean hasError() {
		return this.codeNum != 0;
	}

	public boolean hasErrno() {
		return this.codeNum !=1;
	}
	// 添加错误，用于alertError
	public AjaxResult addError(String message) {
		this.message = message;
		this.codeNum = 1;
		return this;
	}

	/**
	 * 用于Confirm的错误信息
	 * @return AjaxResult
	 */
	public AjaxResult addConfirmError(String message) {
		this.message = message;
		this.codeNum = 2;
		return this;
	}

	/**
	 * 封装成功时的数据
	 * @param data
	 * @return AjaxResult
	 */
	public AjaxResult success(Object data) {
		this.data = data;
		return this;
	}

	@Override
	public String toString() {
		return JsonKit.toJson(this);
	}
}
